package com.project.controller.custom;

import com.project.bean.CarBean;
import com.project.bean.UserBean;
import com.project.service.ICarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("custom/shop")
public class ShopController {
    @Autowired
    private ICarService service;

    @RequestMapping("buy")
    public String buy(int productId, HttpServletRequest request){
        //从会话中，得到登录信息
        UserBean user = (UserBean) request.getSession().getAttribute("loginUser");
        //调用业务方法，完成购物车的添加
        service.add(productId,user.getId());
        return "ok";
    }

    /**
     * 查询登录用户的购物信息
     * @param request 请求对象
     * @return 购物信息
     */
    @RequestMapping("findByUser")
    public List<CarBean> findByUser(HttpServletRequest request){
        //从session中得到登录用户
        UserBean user = (UserBean) request.getSession().getAttribute("loginUser");
        return service.findByUser(user.getId());

    }
    @RequestMapping("del")
    public String del(int carId){
        service.del(carId);
        return "ok";
    }
    @RequestMapping("update")
    public String update(int carId,int  newNum){
        service.updateNum(carId,newNum);
        return "ok";
    }


}
